import { $http, $helper } from '@/utils';
import { $ResponseCode } from '@/utils/const/env';
export const Upload = {
    upload: (data: any) => {
        return new Promise((resolve, reject) => {
            if (!data.name) {
                data.name = 'file';
            }
            $http.upload('Upload/upload', data).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch((err: any) => {
                console.log(err);
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    chooseImage: () => {
        return new Promise((resolve, reject) => {
            $helper.chooseImage({
                count: 1,
                success: (res: any) => {
                    $http.upload('Upload/upload', {
                        name: 'file',
                        filePath: res.tempFilePaths[0],
                        formData: {
                            is_hide: 1
                        }
                    }).then((res: any) => {
                        if (res.code === $ResponseCode.SUCCESS) {
                            resolve(res);
                        } else {
                            reject(res);
                        }
                    }).catch(() => {
                        reject({ msg: '服务器繁忙，请稍后再试' });
                    })
                },
                fail: (err: any) => {
                    console.log(err);
                    // reject({ msg: '选择图片失败' });
                }
            })
        })
    }
}